#include "stdafx.h"
#include <iostream>
#include "TCPClient.h"
#include <sstream>

using namespace std;

int Client::SendCommmand(int YCor, int XCor){

	int ret = 0;

	//ComStruct pointer a pointer that points to the ComStruct.
	ComStruct *Message;

	//Reserve memory space the size of ComStruct for the Message ComStruct.
	Message = (ComStruct *) malloc(sizeof(ComStruct));


	if((LastSendY != YCor) && (LastSendX != XCor)){

		cout << "y: " << YCor << endl;
		cout << "x: " << XCor << endl;
		//Convert to network byte order
		(*Message).y = 	htons(YCor);
		(*Message).x = 	htons(XCor);

		//Cast ComStruct to char pointer and send data where the char pointer points to.
		ret = send(sClient, (char*)Message, sizeof(ComStruct), 0);

		LastSendY = YCor;
		LastSendX = XCor;
	};

	return 0;
};

int Client::SendCommmand(int YCor, int XCor, int Height, int Witdth){
	int ret = 0;

	//ComStruct pointer a pointer that points to the ComStruct.
	ComStruct *Message;

	//Reserve memory space the size of ComStruct for the Message ComStruct.
	Message = (ComStruct *) malloc(sizeof(ComStruct));


	if((LastSendY != YCor) && (LastSendX != XCor)){

		cout << "y: " << YCor << endl;
		cout << "x: " << XCor << endl;
		//Convert to network byte order
		(*Message).y = 	htons(YCor);
		(*Message).x = 	htons(XCor);
		(*Message).height= 	htons(Height);
		(*Message).width = 	htons(Witdth);

		//Cast ComStruct to char pointer and send data where the char pointer points to.
		ret = send(sClient, (char*)Message, sizeof(ComStruct), 0);

		LastSendY = YCor;
		LastSendX = XCor;
	};

	return 0;
};


int Client::RecvCommand(){
	int ret = 0;
	bool CommandRecv = false;
	char TCPMessageBack ='8';

	cout << "Waiting for command back" << endl;

	while(!CommandRecv){
		ret = recv(sClient, (char *)TCPMessageBack, sizeof(TCPMessageBack), 0);
		cout << "Ret: " << ret << endl;
		cout << "Message recieved : " << TCPMessageBack << endl;
		if(TCPMessageBack == '1'){
			cout << "Message recieved : " << TCPMessageBack << endl;
			cout << "Command recieved" << endl;
			TCPMessageBack = '0';
			CommandRecv = true;
		};
	};

	return 0;

};

int Client::TCPClientSetup(string Adres, int Port)
{
	cout << "In TCP client setup" << endl;

	string ad;
	ad.c_str();

	WSADATA       wsd;
	int           i;
	struct sockaddr_in server;
	struct hostent    *host = NULL;

	// Parse the command line and load Winsock
	if (WSAStartup(MAKEWORD(2,2), &wsd) != 0){
		cout << "Failed to init Winsock" << endl;
		return 1;
	};

	// Create the socket, and attempt to connect to the server
	sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sClient == INVALID_SOCKET){
		cout << "Failed to create socked: " << WSAGetLastError() << endl;
		return 1;
	};

	// Put interface information in addr struct
	server.sin_family = AF_INET;
	server.sin_port = htons(Port);
	server.sin_addr.s_addr = inet_addr(Adres.c_str());

	if (server.sin_addr.s_addr == INADDR_NONE){
		cout << "No valid  IP adres" << endl;
		return 1;
	};

	cout << "Initial setup done" << endl;

	if (connect(sClient, (struct sockaddr *)&server,sizeof(server)) == SOCKET_ERROR){
		cout << "connect to server failed: " << WSAGetLastError() << endl;
		return 1;
	};

	cout << "Connected done" << endl;

	//cout << "End TCP client setup" << endl;
	return 0;
}
